<?php
class ManageUsersPresenter {
	private $view;
	private $userRepository;
	private $activeUser;
	public function __construct($view, $userRepository, $activeUser) {
		$this->view = $view;
		$this->userRepository = $userRepository;
		$this->activeUser = $activeUser;
	}
	public function getUserList() {
		return $this->userRepository->getAllUsers ();
	}
	public function applyChanges($toDelete, $makeAdmins) {
		// XXX shitterson is some kind of undeletable user current user can't change yourself
		if (in_array ( $this->activeUser->getUserName (), $toDelete )) {
			$this->view->updateError ( "Unfortunately you can't kill yourself. At least here. If you call us we'll give you a gun so don't worry!" );
			return;
		}
		if (in_array ( 'shitterson', $toDelete )) {
			$this->view->updateError ( "Are you kidding? Shitterson is immortal!" );
			return;
		}
		
		// XXX shitterson is always admin current user can't change yourself
		$finalUpdateBatch = array_diff ( $makeAdmins, $toDelete );
		if (! in_array ( $this->activeUser->getUserName (), $finalUpdateBatch )) {
			$this->view->updateError ( "Go to <s>hell</s> plastic surgeon, douchebag! I cannot change you! " );
			return;
		}
		if (! in_array ( 'shitterson', $finalUpdateBatch )) {
			$this->view->updateError ( "All right, you are a dick! How dare you to touch shitterson?" );
			return;
		}
		
		foreach ( $toDelete as $userToDelete ) {
			$this->userRepository->removeUser ( $userToDelete );
		}
		
		$users = $this->getUserList ();
		
		foreach ( $users as $user ) {
			// XXX shitterson is some kind of undeletable user
			if (in_array ( $user->getUserName (), $finalUpdateBatch )) {
				$user->setPrivileges ( User::ADMIN_PRIVILEGES );
			} else {
				$user->setPrivileges ( User::USER_PRIVILEGES );
			}
			
			$this->userRepository->updateUser ( $user );
		}
		
		$this->view->updateSucceed ();
	}
}

?>